convert list into n-dimensional array